package com.itheima.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 短信发送工具类
 */
public class SMSUtils {
	public static void sendSMS(String phoneNumber,Integer code) {
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fd6FTWqMVFkM7hd9mCf", "EsFoI9jxDdbOTx01aVNKcbmr1Fex1Z");
		IAcsClient client = new DefaultAcsClient(profile);

		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		request.putQueryParameter("PhoneNumbers", phoneNumber);
		request.putQueryParameter("SignName", "传智健康");
		request.putQueryParameter("TemplateCode", "SMS_187745754");
		JSONObject json = new JSONObject();
		json.put("code",code);
		request.putQueryParameter("TemplateParam", json.toJSONString());
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		sendSMS("13355659944",123456);
	}
}
